package com.ourcraft.informationcollectionsystem.utils.response;

import java.io.Serializable;

/**
 * 后端返回结果的统一格式
 *
 * @param <T> 主体数据类型
 */
public class Result<T> implements Serializable {
    private Integer code; // 状态码
    private String message; // 状态码描述信息
    private T data; // 主体数据
    private boolean success; // 请求是否成功

    public Result() {
        this.code = 200;
        this.success = true;
    }

    public Result(T data) {
        this.code = 200;
        this.data = data;
        this.success = true;
    }

    public Result(ResultCode resultCode) {
        this.code = resultCode.getCode();
        this.message = resultCode.getMessage();
        this.success = false;
    }

    public Result(ResultCode resultCode, String message) {
        this.code = resultCode.getCode();
        this.message = message;
        this.success = false;
    }

    public static <T> Result<T> success() {
        return new Result<>();
    }

    public static <T> Result<T> success(T data) {
        return new Result<T>(data);
    }

    public static <T> Result<T> fail(ResultCode resultCode, String message) {
        return new Result<T>(resultCode, message);
    }

    public static <T> Result<T> fail(ResultCode resultCode) {
        return new Result<T>(resultCode);
    }

    public static <T> Result<T> fail(String message) {
        return new Result<T>(ResultCode.FAIL, message);
    }

    public static <T> Result<T> fail() {
        return new Result<T>(ResultCode.FAIL);
    }

    public Integer getCode() {
        return code;
    }

    public void setCode(Integer code) {
        this.code = code;
    }

    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }

    public T getData() {
        return data;
    }

    public void setData(T data) {
        this.data = data;
    }

    public boolean isSuccess() {
        return success;
    }

    public void setSuccess(boolean success) {
        this.success = success;
    }

    @Override
    public String toString() {
        return "Result{" +
                "success=" + success +
                ", code=" + code +
                ", message='" + message + '\'' +
                ", data=" + data +
                '}';
    }
}
